package cn.edu.ctbu.datajpa01.webapi;

import cn.edu.ctbu.datajpa01.core.PageUtils;
import cn.edu.ctbu.datajpa01.domain.Banji;

import cn.edu.ctbu.datajpa01.service.BanjiService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;

import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/webapi/banji")
public class BanjiRestController {

    @Autowired
    public BanjiService banjiService;

    /**
     * 读取的url；/webapi/banji/list
     * @return
     */
    @GetMapping("/list")
    public List<Banji> getAll(){

        List<Banji> banjis=banjiService.findAll();
        return banjis;

    }


    /**
     * 分页读取全部内容
     * @return
     */
    @GetMapping("/getbypage")
    public PageUtils getByPage(@RequestParam(value = "page",defaultValue = "0") Integer page,
                               @RequestParam(value = "size",defaultValue = "10") Integer size,
                               @RequestParam(value = "name",defaultValue = "") String name){

        //按id排倒序
        Sort sort=Sort.by(Sort.Direction.ASC,"id");

        Page<Banji> banjiPage;

        if(StringUtils.isEmpty(name))
        {
            Pageable pageable=PageRequest.of(page,size,sort);
            banjiPage=banjiService.findAll(pageable);
        }

        else
        {
            Banji banji=new Banji();
            banji.setCname(name);

            Pageable pageable=PageRequest.of(page,size,sort);

            ExampleMatcher matcher=ExampleMatcher.matching()
                    .withMatcher("name",ExampleMatcher.GenericPropertyMatchers.contains());

            Example<Banji> example=Example.of(banji,matcher);

            banjiPage=banjiService.findAll(example,pageable);
        }

        PageUtils pageUtils=new PageUtils(banjiPage.getContent(),banjiPage.getTotalElements());

        return pageUtils;
    }


    /**
     * 新增学生的方法 /webapi/banji/insert
     * @param banji
     * @return
     */
    @PostMapping("/insert")
    public Banji insert(Banji banji){

        Banji banji1=banjiService.insert(banji);
        return banji1;

    }

    /**
     * 更新学生的方法 /webapi/banji/update
     * @param banji
     * @return
     */
    @PutMapping("/update")
    public Banji update(Banji banji){

        Banji old=banjiService.getById(banji.getId()); //读取旧的数据
        if(StringUtils.isEmpty(banji.getCno())){

            banji.setCno(old.getCno());

        }
        banji=banjiService.update(banji);
        return banji;
    }

    /**
     * 删除学生的方法 /webapi/banji/delete
     *
     * @param id
     */
    @DeleteMapping ("/delete/{id}")
    public void delete(@PathVariable Integer id){

        banjiService.delete(id);

    }

    /**
     * 读取的url；/webapi/banji/get/id
     * @return
     */
    @GetMapping("/get/{id}")
    public Banji get(@PathVariable Integer id){

        Banji banji=banjiService.getById(id);

        return banji;

    }

}
